package com.itboye.ihomebank.activity.myzhujia.qianbao;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.ihomebank.R;
import com.itboye.ihomebank.base.BaseOtherActivity;
import com.itboye.ihomebank.bean.ResultEntity;
import com.itboye.ihomebank.constants.SPContants;
import com.itboye.ihomebank.presenter.UserPresenter;
import com.itboye.ihomebank.util.ByAlert;
import com.itboye.ihomebank.util.OnPasswordInputFinish;
import com.itboye.ihomebank.util.PasswordView;
import com.itboye.ihomebank.util.SPUtils;

import java.util.Observable;
import java.util.Observer;

/**
 * 设置支付密码（完成设置）
 * 
 */
public class ActivityZhiFuPassSheZhiTwo extends BaseOtherActivity implements
		Observer {
	View v_statusbar;
	TextView add_shap_title_tv;
	ImageView close_icon;

	PasswordView pwdView;// 支付密码
	Intent intent;
	String pass;

	UserPresenter userPresenter;

	String uid;

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.close_icon:
			finish();
			break;

		default:
			break;
		}

	}

	@Override
	protected int layout_id() {
		return R.layout.activity_zhifu_pass_shezhi02;
	}

	@Override
	protected void onInitController() {
		final PasswordView pwdView = (PasswordView) findViewById(R.id.pwd_view);

		// 添加密码输入完成的响应
		pwdView.setOnFinishInput(new OnPasswordInputFinish() {
			@Override
			public void inputFinish() {
				// 输入完成后跳转
				// 也就是说——>实现你的交易逻辑什么的在这里写

				if (pass.equals(pwdView.getStrPassword())) {
					userPresenter.updataZhiFu(uid, "", pwdView.getStrPassword());
				} else {
					ByAlert.alert("两次密码输入不一致");
				}
			}
		});

	}

	@Override
	protected void onInit() {
		setStatusBarHeight(v_statusbar);
		add_shap_title_tv.setText("设置支付密码");

		intent = this.getIntent();
		if (intent != null) {
			pass = intent.getStringExtra("pass");
		}
		uid = (String) SPUtils.get(this, null, SPContants.USER_ID, "");

		userPresenter = new UserPresenter(this);

	}

	@Override
	protected void onReadIntent(Bundle bundle) {
		// TODO Auto-generated method stub

	}

	@Override
	public void update(Observable observable, Object data) {
		ResultEntity entity = handlerError(data);

		if (entity != null) {
			if (entity.getEventType() == UserPresenter.zhiFuPass_success) {
				ByAlert.alert(entity.getData());

				SPUtils.put(this, null, SPContants.IS_ZHIFU_PASS, true);
				SPUtils.put(this, null, SPContants.WalletPass, pass);
				ActivityZhiFuPassSheZhi.getInstance().finish();
				finish();
			}
			if (entity.getEventType() == UserPresenter.zhiFuPass_fail) {
				ByAlert.alert(entity.getData());

			}
		}

	}

}
